package atguigu.urlconn;

import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class UpFile {

    public static void main(String[] args) {
        try {
            String BOUNDARY = java.util.UUID.randomUUID().toString();
            String TWO_HYPHENS = "--";
            String LINE_END = "\r\n";

            URL url = new URL("");
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("POST");
            connection.setDoOutput(true);
            connection.setDoInput(true);
            connection.setUseCaches(false);

            // 设置请求头
            connection.setRequestProperty("Connection","Keep-Alive");
            connection.setRequestProperty("Charset","UTF-8");

            // BOUNDARY 用来标识边界
            connection.setRequestProperty("Content-Type","multipart/form-data; BOUNDARY=" + BOUNDARY);
//            connection.setRequestProperty("Authorization","Bearer "+UserInfoConfigure.authToken);
            connection.connect();

            DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream());
            StringBuffer strBufparam = new StringBuffer();

            // 封装键值对数据一
            strBufparam.append(TWO_HYPHENS);
            strBufparam.append(BOUNDARY);
            strBufparam.append(LINE_END);
            strBufparam.append("Content-Disposition: form-data; name=\"" + "groupId" + "\"");
            strBufparam.append(LINE_END);
            strBufparam.append("Content-Type: " + "text/plain" );
            strBufparam.append(LINE_END);
            strBufparam.append("Content-Lenght: "+("").length());
            strBufparam.append(LINE_END);
            strBufparam.append(LINE_END);
            strBufparam.append("");
            strBufparam.append(LINE_END);

            // 封装键值对数据二
            strBufparam.append(TWO_HYPHENS);
            strBufparam.append(BOUNDARY);
            strBufparam.append(LINE_END);
            strBufparam.append("Content-Disposition: form-data; name=\"" + "title" + "\"");
            strBufparam.append(LINE_END);
            strBufparam.append("Content-Type: " + "text/plain" );
            strBufparam.append(LINE_END);
            strBufparam.append("Content-Lenght: "+"kwwl".length());
            strBufparam.append(LINE_END);
            strBufparam.append(LINE_END);
            strBufparam.append("kwwl");
            strBufparam.append(LINE_END);

            // 拼接完成后，一块写入
            outputStream.write(strBufparam.toString().getBytes());

            // 拼接文件的参数
            StringBuffer strBufFile = new StringBuffer();
            strBufFile.append(LINE_END);
            strBufFile.append(TWO_HYPHENS);
            strBufFile.append(BOUNDARY);
            strBufFile.append(LINE_END);
            strBufFile.append("Content-Disposition: form-data; name=\"" + "image" + "\"; filename=\"" + "\"");
            strBufFile.append(LINE_END);
            strBufFile.append("Content-Type: " + "image/*" );
            strBufFile.append(LINE_END);
            strBufFile.append("Content-Lenght: ");
            strBufFile.append(LINE_END);
            strBufFile.append(LINE_END);

            outputStream.write(strBufFile.toString().getBytes());

            // 写入文件
            FileInputStream fileInputStream = new FileInputStream(new File(""));
            byte[] buffer = new byte[1024*2];
            int length = -1;
            while ((length = fileInputStream.read(buffer)) != -1){
                outputStream.write(buffer,0,length);
            }
            outputStream.flush();
            fileInputStream.close();

            // 写入标记结束位
            byte[] endData = (LINE_END + TWO_HYPHENS + BOUNDARY + TWO_HYPHENS + LINE_END).getBytes();//写结束标记位
            outputStream.write(endData);
            outputStream.flush();

            // 得到响应
            int responseCode = connection.getResponseCode();
            if(responseCode == HttpURLConnection.HTTP_OK){
                InputStream inputStream = connection.getInputStream();
//                String result = is2String(inputStream);//将流转换为字符串。
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
